package me.tuyou.stream;

import org.apache.flink.api.common.functions.AggregateFunction;
import org.apache.flink.api.common.functions.FoldFunction;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

public class FoldTest {

    public static void main(String[] args) throws Exception {

        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        DataStreamSource<Tuple2<String, Long>> input = env.fromElements(
                new Tuple2<>("BMW", 2L),
                new Tuple2<>("BMW", 2L),
                new Tuple2<>("Tesla", 3L),
                new Tuple2<>("Tesla", 4L)
        );

        input.keyBy(0)
                .countWindow(2)
                .fold("", new FoldFunction<Tuple2<String, Long>, String>() {
                    @Override
                    public String fold(String accumulator, Tuple2<String, Long> value) throws Exception {
                        return accumulator + value.f1;
                    }
                })
                .print();

        env.execute();
    }
}
